<?php
/* ******************************
 *	SEOoooY aJax actions
 * ******************************
 */

include('../../includes/classes/core.php');
$action = $_REQUEST['act'];
$data = '';

switch ($action) {
    case 'opt':
	    $rResult = mysql_query("SELECT `name` FROM `production_category` WHERE parent_id!=0 AND actived=1 ORDER BY `name` DESC");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
		
				/* General output */
				$data[] = htmlspecialchars_decode($aRow[0], ENT_QUOTES);
				//$s++;
	
		}

        break;
        case 'pos':
            $rResult = mysql_query("SELECT  `production`.`code`,
										`production`.`name`
								FROM `production` LEFT JOIN `pricelist`
								ON `production`.`id` = `pricelist`.`production_id` LEFT JOIN `reserve`
								ON `production`.`id` = `reserve`.`production_id`
								WHERE `production`.`id` IN (SELECT `production_id` FROM `pricelist`)
								GROUP BY `reserve`.`production_id`
								HAVING SUM(`reserve`.`quantity` - `reserve`.`sold`) >= 1");
        
            $data = array();
        
            $s = 0;
            while($aRow = mysql_fetch_array($rResult))
            {
                for ( $i = 0 ; $i < 2; $i++ )
                {
                    /* General output */
                    $data[] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
                   // $s++;
                }
            }
        
            break;
    case 'pricelist_prod_name':
    	$rResult = mysql_query("SELECT 				`name`
								FROM 				`production`
								LEFT JOIN 			`reserve`
								ON      			`production`.`id` = `reserve`.`production_id`
								WHERE 				`production`.`id` NOT IN(SELECT `production_id` FROM `pricelist`) && reserve.price > 0
								GROUP BY   			`production`.`id`
    			               ");

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}

    	break;
    case 'overhead_goods_code':
    	$object_id = $_REQUEST['obj_id'];

    	$rResult = mysql_query("SELECT `P`.`code`
						        FROM `production` AS `P` INNER JOIN `pricelist` AS `PL`
									ON `P`.`id` = `PL`.`production_id` LEFT JOIN `reserve` AS `R`
									ON `P`.`id` = `R`.`production_id`
								WHERE `R`.`object_id` = $object_id
								GROUP BY `P`.`code`
								HAVING SUM(`R`.`quantity`) != SUM(`R`.`sold`)
    			               ");

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = $aRow[$i];
    			$s++;
    		}
    	}

    	break;

    case 'overhead_goods_name':
    	$object_id = $_REQUEST['obj_id'];

    	if(empty($object_id)){
	    	$rResult = mysql_query("SELECT `name`
							        FROM `production`");
    	}else{
	    	$rResult = mysql_query("SELECT `P`.`name`
							        FROM `production` AS `P` INNER JOIN `pricelist` AS `PL`
										ON `P`.`id` = `PL`.`production_id` LEFT JOIN `reserve` AS `R`
										ON `P`.`id` = `R`.`production_id`
									WHERE `R`.`object_id` = $object_id
									GROUP BY `P`.`code`
									HAVING SUM(`R`.`quantity`) != SUM(`R`.`sold`)
	    			               ");
    	}

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}

    	break;

	case 'buyng_goods_code':

		$rResult = mysql_query("SELECT	`code`
								FROM 	`production`");

		$data = array();
		$s = 0;

		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

    	break;
	case 'buyng_goods_name':

		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`");

		$data = array();
		$s = 0;

		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'overhead_goods_local_name':
    	$overhead_id = $_REQUEST['id'];

		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`");

		$data = array();
		$s = 0;

		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
    case 'overhead_driver_tin':
		$rResult = mysql_query("SELECT	`PR`.`user_id`
								FROM 	`persons` AS `PR` LEFT JOIN `position` AS `PO`
									ON  `PR`.`position` = `PO`.`id`
								WHERE 	`PR`.`position` = 9 && `PO`.`visible` = 'Yes' ");

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = $aRow[$i];
    			$s++;
    		}
    	}

    	break;
    case 'overhead_driver_name':
		$rResult = mysql_query("SELECT	`PR`.`name`
								FROM 	`persons` AS `PR` LEFT JOIN `position` AS `PO`
									ON  `PR`.`position` = `PO`.`id`
								WHERE 	`PR`.`position` = 9 && `PO`.`visible` = 'Yes' ");

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}

    	break;
    case 'overhead_buyer_tin':
    	$rResult = mysql_query("SELECT	`rs_id`
						        FROM 	`partners`
    			               ");

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = $aRow[$i];
    			$s++;
    		}
    	}

    	break;
    case 'overhead_buyer_name':
    	$rResult = mysql_query("SELECT	`name`
						        FROM 	`partners`
    			               ");

    	$data = array();

    	$s = 0;
    	while($aRow = mysql_fetch_array($rResult))
    	{
    		for ( $i = 0 ; $i < 1; $i++ )
    		{
    			/* General output */
    			$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
    			$s++;
    		}
    	}

    	break;
	case 'identity_name':
		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`");

		$data = array();
		$s = 0;

		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'recipie_name':
		$rResult = mysql_query("SELECT	`name`
					       		FROM 	`production`
								WHERE	`category` = 1
		              ");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;

	case 'order_recipie_name':
		$rResult = mysql_query("SELECT	`name`
								FROM 	`production`
								WHERE	`name` IN(SELECT `recipie`.`recipie_name` FROM `recipie`) && `category`= 1
	              ");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'production_name':
		$rResult = mysql_query("SELECT	`name`
					       		FROM 	`production`
								WHERE   `category` != 1");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'transfer_production_name':
    	$object_id = $_REQUEST['obj_id'];

		$rResult = mysql_query("SELECT 	CONCAT(production.`name`,' (',ROUND(SUM(reserve.quantity-reserve.sold),3),' ',production_unit.`name`,')') AS `name`
								FROM 	`production`
								JOIN  production_unit ON production_unit.id=production.unit
								JOIN  reserve ON reserve.production_id=production.id
								WHERE reserve.`object_id` = $object_id AND reserve.quantity - reserve.sold > 0 AND ISNULL(reserve.barcode)
								GROUP BY production.id");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'cartridge_pricelist':
    	$client_id = $_REQUEST['id'];

		$rResult = mysql_query("SELECT DISTINCT `production`.`name`
								FROM			`cartridge_identity`
								LEFT JOIN		`production` ON `cartridge_identity`.`production_id` = `production`.`id`
								WHERE			`cartridge_identity`.`production_id` NOT IN (SELECT cartridge_pricelist.production_id FROM cartridge_pricelist WHERE `cartridge_pricelist`.`client_id`= $client_id && `cartridge_pricelist`.`actived` = 1)");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'printer_pricelist':
		$rResult = mysql_query("SELECT DISTINCT `production`.`name`
								FROM 			`printer_identity` LEFT JOIN `production`
								ON 				`printer_identity`.`production_id` = `production`.`id`");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;

	case 'client_objects':
		$client_id = $_REQUEST['cid'];

		if( !empty($client_id) && $client_id != 'undefined') {
			$rResult = mysql_query("SELECT		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `name`
									FROM 		`client_objects`
									LEFT JOIN 	`client` ON `client`.`id` = `client_objects`.`client_id`
									WHERE		`client_objects`.`actived` = 1 AND `client`.`id` = '$client_id'");
		}else{
			$rResult = mysql_query("SELECT		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `name`
									FROM 		`client_objects`
									LEFT JOIN 	`client` ON `client`.`id` = `client_objects`.`client_id`
									WHERE		`client_objects`.`actived` = 1");
		}

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;

	case 'client_cartridge':
    	$client_id = $_REQUEST['id'];

		$rResult = mysql_query("SELECT DISTINCT	`production`.`name`
								FROM			`cartridge_identity`
								LEFT JOIN       `production` ON `production`.`id` = `cartridge_identity`.`production_id`
								LEFT JOIN       `cartridge_pricelist` ON `cartridge_pricelist`.`production_id` = `cartridge_identity`.`production_id` && `cartridge_pricelist`.`parts_type`= `cartridge_identity`.`parts_type`
								WHERE			`cartridge_pricelist`.`client_id` = $client_id && `cartridge_pricelist`.`actived` = 1");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'client_serial_number':
			$client_id = $_REQUEST['id'];
			$cart_name = $_REQUEST['cart_name'];
			$rResult = mysql_query("SELECT 	barcodes.`code`
									FROM 	barcodes
									JOIN    production ON CAST(SUBSTRING(barcodes.`code`,6,4) as UNSIGNED) = production.id
									WHERE   CAST(SUBSTRING(barcodes.`code`,2,4) as UNSIGNED)=$client_id AND production.`name`='$cart_name'");

			$data = array();

			$s = 0;
			while($aRow = mysql_fetch_array($rResult))
			{
				for ( $i = 0 ; $i < 1; $i++ )
				{
					/* General output */
					$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
					$s++;
				}
			}

			break;
	case 'client_printer':
		$client_id = $_REQUEST['id'];

		$rResult = mysql_query("SELECT DISTINCT	`production`.`name`
                                FROM			`production`
                                JOIN      `printer_pricelist` ON `printer_pricelist`.`production_id` = `production`.`id` 
                                WHERE			`printer_pricelist`.`client_id` = $client_id && `printer_pricelist`.`actived` = 1");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'barcode_client':
		$rResult = mysql_query("SELECT	`name`
					       		FROM 	`client`
								WHERE	`actived` = 1");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'barcode_cartridge':
		$rResult = mysql_query("SELECT	`PR`.`name`
								FROM	`production` AS `PR`
								WHERE	`PR`.`id` in (SELECT `production_id` FROM `cartridge_identity`) && `PR`.`actived` = 1");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'barcode_printer':
		$rResult = mysql_query("SELECT	`PR`.`name`
							FROM	`production` AS `PR`
							WHERE	`PR`.`id` in (SELECT `production_id` FROM `printer_identity`) && `PR`.`actived` = 1");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
	case 'a_letters':
		$rResult = mysql_query("SELECT		CONCAT(`client`.`name`, '(',`client_objects`.`name`, ')', ' №: ა-',`a_letter`.`id`)
								FROM 		`a_letter`
								LEFT JOIN 	`client_objects` ON `client_objects`.`id` = `a_letter`.`client_object_id`
								LEFT JOIN 	`client` ON `client`.`id` = `client_objects`.`client_id`  && `a_letter`.`id` not in (SELECT `a_letter_id` FROM `work_blank` WHERE `actived` = 1 && a_letter_id!='') ");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
					$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
					$s++;
			}
		}

		break;
	case 'describe_production':
		$describe_id = $_REQUEST ['des'];
		$object_id = $_REQUEST ['ob'];
		 $rResult = mysql_query ( "	SELECT  		`production`.`name` AS `name`
									FROM  			`reserve`
									LEFT JOIN     	`production` ON	`reserve`.`production_id` = `production`.`id`
									LEFT JOIN     	`production_unit` ON `production`.`unit` = `production_unit`.`id`
									WHERE			reserve.quantity - reserve.sold > 0 AND ISNULL(reserve.barcode)
		                                             AND  production.id NOT IN(SELECT describe_detail.production_id FROM describe_detail WHERE describe_detail.describe_id = '$describe_id') 
		                                             AND `reserve`.`object_id` = '$object_id'
									GROUP BY     	`reserve`.`object_id`, `reserve`.`production_id`"); 
/* 		$rResult = mysql_query ( "SELECT  		`production`.`name`
									FROM  			`production`");
 */
		$data = array ();

		$s = 0;
		while ( $aRow = mysql_fetch_array ( $rResult ) ) {
			for($i = 0; $i < 1; $i ++) {
				/* General output */
				$data [$s] = htmlspecialchars_decode ( $aRow [$i], ENT_QUOTES );
				$s ++;
			}
		}

		break;
	case 'post_codes':
		$rResult = mysql_query ("	SELECT	CONCAT(`postal_codes`.`code`, ' - ', `postal_codes`.`name`)
									FROM	postal_codes
									WHERE	postal_codes.district_id = '' || ISNULL( postal_codes.district_id )

									UNION

									SELECT	DISTINCT `postal_codes`.`code`
									FROM		`postal_codes`
									WHERE		`postal_codes`.`district_id` = '' || ISNULL( postal_codes.district_id )");

		$data = array ();

		$s = 0;
		while ( $aRow = mysql_fetch_array ( $rResult ) ) {
			for($i = 0; $i < 1; $i ++) {
				/* General output */
				$data [$s] = htmlspecialchars_decode ( $aRow [$i], ENT_QUOTES );
				$s ++;
			}
		}

		break;
	case 'postal_codes':
		$address_name = $_REQUEST['add'];
		$address_name = trim($address_name);
		$rResult = mysql_query ("	SELECT	CONCAT(`postal_codes`.`code`, ' - ', `postal_codes`.`name`)
									FROM	postal_codes
									WHERE	`postal_codes`.`name` LIKE '%$address_name%'");

		$data = array ();

		$s = 0;
		while ( $aRow = mysql_fetch_array ( $rResult ) ) {
			for($i = 0; $i < 1; $i ++) {
				/* General output */
				$data [$s] = htmlspecialchars_decode ( $aRow [$i], ENT_QUOTES );
				$s ++;
			}
		}

		break;
	case 'obj_address':
		$rResult = mysql_query ("	SELECT	`name`
									FROM		`addresses`");

		$data = array ();

		$s = 0;
		while ( $aRow = mysql_fetch_array ( $rResult ) ) {
			for($i = 0; $i < 1; $i ++) {
				/* General output */
				$data [$s] = htmlspecialchars_decode ( $aRow [$i], ENT_QUOTES );
				$s ++;
			}
		}

		break;
	case 'per_contact_person':
		$client_name = $_REQUEST['cid'];
		$id = getClientObjectID($client_name);

		$rResult = mysql_query ("	SELECT		`client_object_persons`.`name`
									FROM		`client_object_persons`
									LEFT JOIN	`client_objects` ON `client_objects`.`id` = `client_object_persons`.`client_object_id`
									WHERE		`client_object_persons`.`actived` =1 AND `client_objects`.`actived` = 1 AND`client_objects`.`id` = $id");

		$data = array();

		$s = 0;
		while($aRow = mysql_fetch_array($rResult))
		{
			for ( $i = 0 ; $i < 1; $i++ )
			{
				/* General output */
				$data[$s] = htmlspecialchars_decode($aRow[$i], ENT_QUOTES);
				$s++;
			}
		}

		break;
    default:
       echo 'Action is Null';
}

echo json_encode($data);

function getClientObjectID($client_name){

	$res = mysql_fetch_assoc(mysql_query("	SELECT		`client_objects`.`id`
											FROM		`client_objects`
											LEFT JOIN	`client` ON `client`.`id` = `client_objects`.`client_id`
											WHERE		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') = '$client_name'"));
	return $res['id'];
}

?>